/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.citi.dahg.cve.controller.service;

import com.citi.dahg.cve.controller.util.Encrypt;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Query;
/**
 *
 * @author dh90940
 */

public abstract class BasicService<T> extends AbstractStatelessBeanCVE implements Service<T> {        

    protected abstract Class<T> getClazz();
    
    @Override    
    public void add(T obj) {
        getEntityManager().persist(obj);
    }

    @Override    
    public void update(T obj) {
        getEntityManager().merge(obj);
    }

    @Override
    public void delete(T obj) {
       getEntityManager().remove(getEntityManager().merge(obj));
    }

    public Encrypt getDecrypt() {
        return decrypt;
    }    

    protected abstract String queryGetAll();
    
    @Override
    public List<T> getAll() {
        Query query = getEntityManager().createQuery(queryGetAll());
        List<T> lista = new ArrayList<T>();
        
        lista.addAll(query.getResultList());
        return lista;
    }

    @Override    
    public T findById(Object id) {
        return getEntityManager().find(getClazz(), id);
    }
    
    
}
